import ZhuanKuai from '../../assets/materialImg/ZhuanKuai.jpeg'

const geologicalExcavation = function(params) {
  /**
        * https://blog.csdn.net/renkangever/article/details/109739366
          https://www.cnblogs.com/wanghui2011/articles/12924464.html
        * @type {Globe|*|globe|{get, set}|{get}}
        */
  const globe = viewer.scene.globe
  var points = []; var polygon
  // 定义canvas屏幕点击事件
  var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas)
  // 注册鼠标事件,event参数是点击的地方是在哪里
  handler.setInputAction((event) => {
    const pickRay = viewer.camera.getPickRay(event.position)
    var position = globe.pick(pickRay, viewer.scene)
    points.push(position)
  }, Cesium.ScreenSpaceEventType.LEFT_CLICK)

  // 注册鼠标事件,event参数是点击的地方是在哪里
  handler.setInputAction((event) => {
    if (polygon) {
      viewer.entities.remove(polygon)
    }
    const hierarchy = []
    const clippingPlanes = []
    for (let i = 0; i < points.length; i++) {
      const point = points[i]
      // 将笛卡尔坐标转化为弧度坐标
      var cartographic = Cesium.Cartographic.fromCartesian(point)
      // 将弧度坐标转换为经纬度坐标
      var longitude = Cesium.Math.toDegrees(cartographic.longitude) // 经度
      var latitude = Cesium.Math.toDegrees(cartographic.latitude) // 纬度
      var height = cartographic.height // 高度
      hierarchy.push(longitude)
      hierarchy.push(latitude)
      hierarchy.push(height)
    }
    polygon = viewer.entities.add({
      polygon: {
        hierarchy: Cesium.Cartesian3.fromDegreesArrayHeights(hierarchy),
        material: new Cesium.ImageMaterialProperty({
          image: ZhuanKuai,
          repeat: new Cesium.Cartesian2(3.0, 3.0)
        }),
        closeTop: false, // 顶部不关闭
        extrudedHeight: 0, // 200,
        perPositionHeight: true // 使用每个位置的高度
      }
    })

    var pointsLength = points.length

    for (var i = 0; i < pointsLength; i++) {
      var nextIndex = (i + 1) % pointsLength
      // 计算相互两个点直接的中点
      var midpoint = Cesium.Cartesian3.add(points[i], points[nextIndex], new Cesium.Cartesian3())
      midpoint = Cesium.Cartesian3.multiplyByScalar(midpoint, 0.5, midpoint)
      // 将中点归一化
      var up = Cesium.Cartesian3.normalize(midpoint, new Cesium.Cartesian3())

      // 计算出右侧的方向
      var right = Cesium.Cartesian3.subtract(points[nextIndex], midpoint, new Cesium.Cartesian3())
      right = Cesium.Cartesian3.normalize(right, right)

      // 利用外积计算两个向量的法向量，并将法向量归一化，也就是计算出来剖切面的朝向
      var normal = Cesium.Cartesian3.cross(right, up, new Cesium.Cartesian3())
      normal = Cesium.Cartesian3.normalize(normal, normal)

      // 根据法向量创建剖切面
      var originCenteredPlane = new Cesium.Plane(normal, 0.0)
      // 计算平面到中点之间的距离
      var distance = Cesium.Plane.getPointDistance(originCenteredPlane, midpoint)
      // 确定剖切面的法向量与距离
      clippingPlanes.push(new Cesium.ClippingPlane(normal, distance))
    }
    // 设置地球地质的剖切面集合
    globe.clippingPlanes = new Cesium.ClippingPlaneCollection({
      planes: clippingPlanes,
      edgeColor: Cesium.Color.RED,
      edgeWidth: 2.0,
      enabled: true,
      unionClippingRegions: false// 设置为true，即取并集,false 交集
    })
    // 初始化取点数据
    points.splice(0, points.length)
    handler.destroy()
  }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)

  return geologicalExcavation
}

export default geologicalExcavation
